package com.zf.shuashua.ui.mine.notice;

import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.lxj.xpopup.XPopup;
import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.NoticeBean;
import com.zf.shuashua.bean.NoticeList;
import com.zf.shuashua.bean.RewardPoint;
import com.zf.shuashua.ui.gift.H5Activity;
import com.zf.shuashua.ui.popup.OnPopupListener;
import com.zf.shuashua.ui.popup.OpenNoticePopup;
import com.zf.shuashua.utils.CommonConstants;

import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;

/**
 * 系统通知列表
 */
public class NoticeListFragment extends BaseListFragment<NoticeBean> {

    private RecyclerView mRecyclerView;
    private NoticeListAdapter mAdapter;
    private String noticeId;

    public static NoticeListFragment getFragment() {
        return new NoticeListFragment();
    }

    @Override
    protected void initView() {
//        setState(ViewState.EMPTY);
        setEmptyLayout(R.layout.empty_notice_list);
        super.initView();
    }

    @Override
    protected void initData() {
        loadPage(1);
    }

    @Override
    protected Observable<Result<List<NoticeBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getNoticeList(pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<NoticeBean>>> getListData(Observable<String> observable) {
        final Result<List<NoticeBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    NoticeList noticeList = ParseJsonUtils.parseData(s, NoticeList.class);
                    result.data = noticeList.getDataList();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list_search_video;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mRecyclerView = findViewById(R.id.recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ((SimpleItemAnimator) Objects.requireNonNull(mRecyclerView.getItemAnimator())).setSupportsChangeAnimations(false);
        return mRecyclerView;
    }

    @Override
    protected MeiBaseAdapter<NoticeBean> getAdapter() {
        mAdapter = new NoticeListAdapter(R.layout.item_notice, null);
        mAdapter.setListener(noticeBean -> {
            noticeId = noticeBean.getId();
            //消息链接埋点
            HttpData.getInstance().get_record_num1("2", noticeId, new ACallback<BaseData>() {
                @Override
                public void onSuccess(BaseData data) {
                }

                @Override
                public void onFail(int errCode, String errMsg) {

                }
            });
            startActivityForResult(new Intent(mActivity, H5Activity.class).putExtra("money", noticeBean.getUrl() + "").putExtra("title", noticeBean.getTitle() + ""), CommonConstants.XITONGXIAOXI);
        });
        return mAdapter;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConstants.XITONGXIAOXI) {
            HttpData.getInstance().getRewardPoint(noticeId, new ACallback<RewardPoint>() {
                @Override
                public void onSuccess(RewardPoint data) {
                    if (data.isStatus() && data.getCode() == 200) {
                        if (!TextUtils.isEmpty(data.getData().getValue())) {
                            OpenNoticePopup popupView = new OpenNoticePopup(mActivity, data.getData().getValue(), "");
                            popupView.setPopupListener(new OnPopupListener() {
                                @Override
                                public void onNext() {
                                    popupView.dismiss();
                                    mActivity.setResult(RESULT_OK);
                                    mActivity.finish();
                                    //回去去首页看视频
                                }

                                @Override
                                public void onClose() {
                                    popupView.dismiss();
                                }
                            });
                            new XPopup.Builder(mActivity)
                                    .asCustom(popupView)
                                    .show();


                        }
                    }
                }

                @Override
                public void onFail(int errCode, String errMsg) {

                }
            });

        }
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return false;
    }

}
